/*
 * Tencent is pleased to support the open source community by making Tinker available.
 *
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 *
 * https://opensource.org/licenses/BSD-3-Clause
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.andova;

import android.annotation.TargetApi;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.andova.component.AndovaComponentManager;
import com.andova.component.config.AndovaConfigComponent;
import com.andova.component.config.Config;
import com.andova.component.config.ConfigBean;
import com.andova.component.config.Value;
import com.andova.component.config.ValueBean;
import com.andova.component.net.RetrofitComponent;
import com.andova.component.net.RetrofitSusComponent;
import com.andova.manager.AndovaModuleManager;
import com.andova.manager.BooleanA;
import com.andova.manager.SharedPreferenceManager;
import com.andova.tinker.HotfixPatchListener;
import com.andova.tinker.HotfixResultService;
import com.tencent.tinker.anno.DefaultLifeCycle;
import com.tencent.tinker.entry.ApplicationLifeCycle;
import com.tencent.tinker.entry.DefaultApplicationLike;
import com.tencent.tinker.lib.patch.UpgradePatch;
import com.tencent.tinker.lib.reporter.DefaultLoadReporter;
import com.tencent.tinker.lib.reporter.DefaultPatchReporter;
import com.tencent.tinker.lib.tinker.TinkerInstaller;
import com.tencent.tinker.loader.shareutil.ShareConstants;

import java.util.HashSet;
import java.util.Set;

/**
 * because you can not use any other class in your application, we need to
 * move your implement of Application to {@link ApplicationLifeCycle}
 * As Application, all its direct reference class should be in the main dex.
 * <p>
 * We use tinker-android-anno to make sure all your classes can be patched.
 * <p>
 * application: if it is start with '.', we will add SampleApplicationLifeCycle's package name
 * <p>
 * flags:
 * TINKER_ENABLE_ALL: support dex, lib and resource
 * TINKER_DEX_MASK: just support dex
 * TINKER_NATIVE_LIBRARY_MASK: just support lib
 * TINKER_RESOURCE_MASK: just support resource
 * <p>
 * loaderClass: define the tinker loader class, we can just use the default TinkerLoader
 * <p>
 * loadVerifyFlag: whether check files' md5 on the load time, defualt it is false.
 * <p>
 * Created by zhangshaowen on 16/3/17.
 */
@SuppressWarnings("unused")
@DefaultLifeCycle(application = "com.andova.sample.WorkApplication",
        flags = ShareConstants.TINKER_ENABLE_ALL,
        loadVerifyFlag = false)
public class WorkApplicationLike extends DefaultApplicationLike {
    private static final String TAG = "Tinker.SampleApplicationLike";
    private final ModuleOption mModuleOption = new ModuleOption();

    public WorkApplicationLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag,
                               long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
        super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
    }

    /**
     * install multiDex before install tinker
     * so we don't need to put the tinker lib classes in the main dex
     */
    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    @Override
    public void onBaseContextAttached(Context base) {
        super.onBaseContextAttached(base);
        TinkerInstaller.install(this,
                new DefaultLoadReporter(base),
                new DefaultPatchReporter(base),
                new HotfixPatchListener(base),
                HotfixResultService.class,
                new UpgradePatch());
    }

    @Override
    public void onCreate() {
        super.onCreate();
        AndovaModuleManager.obj().initialize(getApplication(), new EmptyOption(), mModuleOption, mModuleOption);
        BooleanA a = SharedPreferenceManager.obj().getBooleanA("test");
        System.out.println("boolean a -> " + a);
        SharedPreferenceManager.obj().editBooleanA("test", true);

        AndovaComponentManager.getInstance()
//                .lbsComponent(new BaiDuComponent())
                .netComponent(new RetrofitComponent())
                .netSusComponent(new RetrofitSusComponent())
                .configComponent(new AndovaConfigComponent(new Config() {
                    @Override
                    protected boolean enable() {
                        return true;
                    }

                    @Override
                    protected ValueBean url() {
                        Set<Value> set = new HashSet<>();
                        set.add(new Value("测试地址", "http://192.168.0.100/test"));
                        set.add(new Value("演示地址", "http://192.168.0.100/demo"));
                        return new ValueBean("http://localhost/test", "测试地址", set);
                    }

                    @Override
                    protected boolean log() {
                        return false;
                    }

                    @Override
                    protected ConfigBean[] node() {
                        Set<Value> set = new HashSet<>();
                        set.add(new Value("临时值1", "temp1"));
                        set.add(new Value("临时值2", "temp2"));
                        return new ConfigBean[]{
                                new ConfigBean("node1", "", set),
                                new ConfigBean("node2", "value2"),
                                new ConfigBean("node3", "value3", set),
                        };
                    }

                    @Override
                    protected int version() {
                        return 1;
                    }
                })).initialize(getApplication());
    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public void registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) {
        getApplication().registerActivityLifecycleCallbacks(callback);
    }
}
